package com.ruiysoft.controller.sms.service;

import com.ruiysoft.constant.ConstantKey;
import com.ruiysoft.controller.sms.dto.GreetingDto;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by 化增光 on 2019/2/23.
 */
@Service
public class GreetingApiService {
    @Resource
    private RedisTemplate<String,GreetingDto> messageTemplate;

    @Resource
    private RedisTemplate<String,Long> greetTemplate;

//    @Resource
//    private ISmsShopGreetRelationService iSmsShopGreetRelationService;

    public void addMessage(String key, GreetingDto message) {

        messageTemplate.opsForZSet().add(ConstantKey.SMS_GREETING_MESSAGE_KEY+key, message,message.getTime());
        //messageTemplate.opsForList().size(ConstantKey.GREETING_MESSAGE_KEY+key);
    }

    public void addGreetings(Long shopId, Long customer, Long customerService) {
        greetTemplate.opsForZSet().add(ConstantKey.SMS_GREETING_PERSONNEL_KEY+shopId+"_"+customer,customerService,System.currentTimeMillis());
        greetTemplate.opsForZSet().add(ConstantKey.SMS_GREETING_PERSONNEL_KEY+shopId+"_"+customerService,customer,System.currentTimeMillis());


//
//        iSmsShopGreetRelationService.insertIgnore(new SmsShopGreetRelation()
//                .setCustomer(customer)
//                .setCustomerService(customerService)
//                .setShopId(shopId));
    }

    public Set<Long> getGreetings(Long shopId, Long personnelId) {
        Set<Long> ids = greetTemplate.opsForZSet().reverseRange(ConstantKey.SMS_GREETING_PERSONNEL_KEY+shopId+"_"+personnelId,0,-1);
        return ids;
    }

    public void addMessage(Long shopId, Long customer, Long customerService, GreetingDto message) {
        StringBuffer key = new StringBuffer()
                .append(shopId)
                .append("_")
                .append(customer)
                .append("_")
                .append(customerService);
        messageTemplate.opsForZSet().add(ConstantKey.SMS_GREETING_MESSAGE_KEY+key.toString(), message,message.getTime());
    }

    public List<GreetingDto> getMessage(@Valid Integer pageStart, @Valid Integer pageSize, Long shopId, Long customer, Long customerService) {
        StringBuffer key = new StringBuffer()
                .append(shopId)
                .append("_")
                .append(customer)
                .append("_")
                .append(customerService);
        Set<ZSetOperations.TypedTuple<GreetingDto>> reverseRangeWithScores = messageTemplate.opsForZSet().reverseRangeWithScores(ConstantKey.SMS_GREETING_MESSAGE_KEY+key.toString(), pageStart, pageStart+pageSize);
        Iterator<ZSetOperations.TypedTuple<GreetingDto>> iterator = reverseRangeWithScores.iterator();
        List<GreetingDto> dtos = new ArrayList<>();
        while(iterator.hasNext()){
            ZSetOperations.TypedTuple<GreetingDto> next = iterator.next();
            next.getValue().setTime(next.getScore().longValue());
            dtos.add(next.getValue());
        }
        return dtos;
    }


}
